package views;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.util.List;

import javax.swing.*;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;

import controllers.Driver;
import controllers.TimeSlot;
import db.DriverDAO;
import db.TimeSlotDAO;

public class ViewDriversFrame extends JFrame {

	private static final int WIDTH=800;
	private static final int HEIGHT=500;
	private static final String TITLE = "View / Edit Drivers";
	private static Object[][] drivers;
	private JButton delete, add, edit, save, clear;
	private static JTable table;
	private static java.util.List<Driver> list;
	private static MyTableModel model;
	private static Container c;
	private static JScrollPane scrollpane;
	private static int editRow;

	public ViewDriversFrame(int xBound, int yBound){
		this.setBounds(xBound+100, yBound+50, WIDTH, HEIGHT);
		this.setResizable(false);
		this.setTitle(TITLE);

		c = this.getContentPane();
		c.setLayout(null);

		scrollpane = new JScrollPane();
		updateRows();

		c.add(scrollpane);

		add = new JButton("Add");
		add.setBounds(25, 425, 100, 30);
		add.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				new AddDriverFrame(getX(),getY());
			}
		});
		c.add(add);

		edit = new JButton("Edit");
		edit.setBounds(135, 425, 100, 30);
		edit.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				if(row!=-1){
					model.editRow(row);
					editRow = row;
					table.editCellAt(row, table.getSelectedColumn());
					table.getCellEditor().addCellEditorListener(new CellEditorListener(){
						@Override
						public void editingCanceled(ChangeEvent e) {
							// TODO Auto-generated method stub	
						}
						@Override
						public void editingStopped(ChangeEvent e) {
							save.setEnabled(true);
							edit.setEnabled(false);
						}
					});
				}
				else{
					System.out.println("Nothing Selected");
				}
			}
		});
		c.add(edit);

		delete = new JButton("Delete");
		delete.setBounds(245, 425, 100, 30);

		delete.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(JOptionPane.showConfirmDialog(getComponent(0), "Are you sure you want to delete? This cannot be undone", "Delete", 0)==0){
					int row = table.getSelectedRow();
					if(row!=-1){
						DriverDAO.deleteDriver(list.get(row));
						updateRows();
					}
					else{
						System.out.println("Nothing selected");
					}
				}
			}
		});
		c.add(delete);

		save = new JButton("Save");
		save.setBounds(355,425,100,30);
		save.setEnabled(false);
		save.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Driver temp = list.get(editRow);
				temp.setName(table.getValueAt(editRow, 0).toString());
				temp.setEmail(table.getValueAt(editRow, 1).toString());
				temp.setPhoneNumber(table.getValueAt(editRow, 2).toString());
				if(DriverDAO.saveDriver(temp)){
					save.setEnabled(false);
					edit.setEnabled(true);
				}
				else{
					JOptionPane.showMessageDialog(getComponent(0), "Save Failed! Please make sure your changes are valid.","Save Failed!",0);
				}
			}
		});

		c.add(save);
		
		clear = new JButton("Clear");
		clear.setBounds(465, 425, 100, 30);
		clear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				updateRows();
				edit.setEnabled(true);
				save.setEnabled(false);
			}
		});
		c.add(clear);

		this.setVisible(true);

	}

	public static void updateRows(){
		list = DriverDAO.getDrivers();
		drivers = new Object[list.size()][3];
		for(int i=0;i<list.size();i++){
			drivers[i][0] = list.get(i).getName();
			drivers[i][1] = list.get(i).getEmail();
			drivers[i][2] = list.get(i).getPhoneNumber();
		}
		Object columnNames[] = {"Name", "Email", "Phone Number"};
		model = new MyTableModel(drivers, columnNames);
		table = new JTable(model);
		table.setRowHeight(30);
		table.getTableHeader().setReorderingAllowed(false);
		if (list.size()>0)
                    table.addRowSelectionInterval(0, 0);
		c.remove(scrollpane);
		scrollpane = new JScrollPane(table);
		scrollpane.setBounds(25,0,750,400);
		c.add(scrollpane);
	}

}
